package com.chen.rabbitmq.three;

import com.chen.rabbitmq.utils.RabbitMqUtils;
import com.chen.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName Work03.java
 * @Description 消息在手动应答是不丢失、放回队列中重新消费
 * @createTime 2023年05月08日 13:15:00
 */
public class Work04 {

    //队列名称
    public static final String TASK_QUEUE_NAME ="ack_queue";

    //接收消息
    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C2等待接收消息处理时间较长");


        //设置不公平分发
//        int prefetchCount = 1;
        //预取值
        int prefetchCount = 5;

        channel.basicQos(prefetchCount);
        //采用手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, (consumerTag, message) -> {
            //沉睡30s
            SleepUtils.sleep(30);
            System.out.println("接收道德消息：" + new String(message.getBody(),"UTF-8"));
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        }, s -> System.out.println(s + "消费者取消消费接口回调"));

    }
}
